package cn.com.wind.bdg.inmysql.inmysql.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName:  JsoupGetCode   
 * @Description:通过jsoup获取地域对应的code   
 * @author: gaoxiang
 * @date:   2020年8月19日 下午2:15:44
 */
public class JsoupGetCode {
	public String method(String area) throws Exception {
		List<String> listRes = new ArrayList<String>();
		String url = "http://10.200.126.163:6667/code";
		String json = "{\"content\": \""+area+"\"}";
		Document doc = Jsoup.connect(url).requestBody(json).header("Content-Type","application/json").ignoreContentType(true).post();
		Element element = doc.body();
		String res = element.text();
		JSONObject jsonObject = JSONObject.parseObject(res);
		JSONArray data = jsonObject.getJSONArray("items");
		//每个code可能不止一个，将code存入list
		for(int i = 0 ;i< data.size();i++) {
            listRes.add(data.getJSONObject(i).getString("code"));        
          }
		//将code拼接成string，以逗号分隔
		StringBuffer stringBuffer = new StringBuffer();
		for(int i=0;i<listRes.size();i++) {
			stringBuffer.append(listRes.get(i)+",");
		}
		return stringBuffer.toString();
		
	}
}
